<?php

namespace app\controller\admin;

use app\common\model\Admin;
use app\common\model\User;
use app\common\service\UserService as Service;
use app\common\util\Result;
use think\annotation\route\Group;
use think\annotation\route\Route;

/**
 * 管理员 控制器类
 */
#[Group('admin/user')]
class UserController extends AdminAuthorizedController
{
    //权限前缀
    protected string $power_prefix = "User";

    /**
     * 初始化业务
     * @return Service
     */
    protected function initService()
    {
        return new Service($this->app);
    }

    #[Route('GET|POST', 'admin_init')]
    public function adminInit(Admin $admin)
    {
        $result["isPassList"] = User::getBindStatusList();
        return Result::success($result);
    }

    /**
     * 后台分页
     * @param Admin $admin
     */
    #[Route('GET|POST', 'page')]
    public function page(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'index');
        return $this->initService()->adminPage(request()->param());
    }

    /**
     * 重置密码
     * @param Admin $admin
     * @return \app\common\model\AdminRole|mixed
     * @throws \Exception
     */
    #[Route('GET|POST', 'resetPassword')]
    public function resetPassword(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'resetPassword');
        $param = request()->param();
        return $this->initService()->resetPassword($param);
    }

    #[Route('GET|POST', 'bindPass')]
    public function bindPass(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'bindPass');
        $param = request()->param();
        $this->validate($param, [
            "id" => "require",
            "bind_status" => "require"
        ]);
        return $this->initService()->bindPass($param);
    }

    #[Route('GET|POST', 'delete')]
    public function delete(Admin $admin)
    {
        $this->checkPower($admin, $this->power_prefix . '_' . 'bindPass');
        $param = request()->param();
        $this->validate($param, [
            "id" => "require",
        ]);
        return $this->initService()->delete($param);
    }
}
